package com.ppx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ppx.dao.UserDao;
import com.ppx.pojo.User;
import com.ppx.service.UserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;

/**
 * @author wangxiaotao
 * @create 2021-05-20-15:51
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private HttpSession session;

    @Override
    public String login(String userAccount, String userPassword) {
        User user=userDao.login(userAccount,userPassword);
        if(user!=null){
            session.setAttribute("imgUrl",user.getUserUrl());
            session.setAttribute("userAccount",userAccount);
            session.setAttribute("userName",user.getUserName());
            if("用户".equals(user.getUserRole())){
                return "shouye";
            }else if("管理员".equals(user.getUserRole())){
                return "index";
            }else
                return "login";
        }
        return "login";
    }

    //查询所有用户
    @Override
    public PageInfo<User> queryAllUser(int pageNum, int pageSize) {
        //使用pagehelper中的startPage
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(userDao.queryAllUser());
    }

    //模糊查询用户
    @Override
    public PageInfo<User> queryUserByAccount(int pageNum, int pageSize, String userAccount) {
        //使用PageHelper插件 startPage开启分页
        PageHelper.startPage(pageNum,pageSize);
        //使用匿名对象返回数据
        return new PageInfo<>(userDao.queryUserByAccount(userAccount));
    }

    //删除用户
    @Override
    public String delUserById(String userAccount) {
        if(userDao.delUserById(userAccount)>0)
            return "success";
        return "error";
    }

    //修改用户
    @Override
    public String updataUserByUserAccount(User user) {
        if(userDao.updataUserByUserAccount(user)==1)
            return "success";
        return "error";
    }

    //查询用户是否存在
    @Override
    public String checkUserAccount(String userAccount) {
        if (userDao.checkUserAccount(userAccount)!=null)
            return "inexistence";
        return "exist";
    }

    //添加用户
    @Override
    public String insertIntoUser(User user) {
        if(userDao.insertIntoUser(user)==1)
            return "success";
        return "error";
    }

    //安全设置
    @Override
    public User updataSelf() {
        return userDao.checkUserAccount((String) session.getAttribute("userAccount"));
    }

    //待恢复用户
    @Override
    public PageInfo<User> queryUserByState(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(userDao.queryUserByState());
    }

    //模糊查询状态异常用户
    @Override
    public PageInfo<User> queryUserByStateByAccount(int pageNum, int pageSize, String userAccount) {
        //使用PageHelper插件 startPage开启分页
        PageHelper.startPage(pageNum,pageSize);
        //使用匿名对象返回数据
        return new PageInfo<>(userDao.queryUserByStateByAccount(userAccount));
    }

    //用户状态设置正常
    @Override
    public String setStateByUserAccount(String userAccount) {
        if(userDao.setStateByUserAccount(userAccount)>0){
            return "success";
        }
        return "error";
    }

    //用户头像修改
    @Override
    public String updataUserImg(String img_url,String userAccount) {
        if(userAccount==null)
            return "error";
        else{
            if (userDao.updataUserImg(img_url,userAccount)>0)
                return "success";
            else
                return "error";
        }
    }
}
